package com.texaco.chapasFutbol.sound;

import java.util.HashMap;
import java.util.Map;

import org.andengine.audio.music.Music;
import org.andengine.audio.sound.Sound;

/**
 * Clase que contendrá la información del sonido.
 * @author texaco
 *
 */
public class SoundHandler {

	Map<String, Sound> fx;
	Map<String, Music> music;
	boolean fxEnable = true;
	boolean musicEnable = true;
	
	public SoundHandler(){
		super();
		fx = new HashMap<String, Sound>();
		music = new HashMap<String, Music>();
	}

	public void putFx(String key, Sound value) {
		fx.put(key, value);
	}

	public void putMusic(String key, Music value) {
		value.setLooping(true);
		music.put(key, value);
	}

	/**
	 * Indica si se está sonando alguna música.
	 * @return
	 */
	public boolean isMusicPlaying() {
		for(Music musicIt : this.music.values()){
			if(musicIt.isPlaying()){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Indica si se está sonando una música en concreto.
	 * @return
	 */
	public boolean isMusicLabelPlaying(String label) {
		if (this.music.containsKey(label)){
			if(this.music.get(label).isPlaying()){
				return true;
			}
		}
		return false;
	}

	/**
	 * Pausa la música que esté sonando.
	 */
	public void pauseMusic() {
		for(Music musicIt : this.music.values()){
			if(musicIt.isPlaying()){
				musicIt.pause();
			}
		}
	}
	
	/**
	 * Pausa una canción en concreto.
	 */
	public void pauseMusicLabel(String label) {
		if (this.music.containsKey(label)){
			if(this.music.get(label).isPlaying()){
				this.music.get(label).pause();
			}
		}
	}

	/**
	 * Detiene una canción en concreto.
	 */
	public void stopMusicLabel(String label) {
		if (this.music.containsKey(label)){
			if(this.music.get(label).isPlaying()){
				this.music.get(label).stop();
			}
		}
	}

	/**
	 * Reanuda la música que esté parada.
	 */
	public void resumeMusic() {
		for(Music musicIt : this.music.values()){
			if(musicEnable){
				musicIt.resume();
			}
		}
	}

	/**
	 * Reanuda una canción en que esté parada.
	 */
	public void resumeMusicLabel(String label) {
		if(musicEnable){
			if (this.music.containsKey(label)){
				this.music.get(label).resume();
			}
		}
	}

	/**
	 * Pone en funcionamiento la música que se pasa por parámetro.
	 * @param musicLabel
	 */
	public void playMusic(String musicLabel, boolean loop) {
		if(musicEnable){
			music.get(musicLabel).setLooping(loop);
			music.get(musicLabel).play();
		}
	}

	/**
	 * Pone en funcionamiento un sonido.
	 * @param soundLabel
	 */
	public void playFx(String soundLabel) {
		if(fxEnable){
			this.fx.get(soundLabel).play();
		}
	}

	public void toogleMusic() {
		this.musicEnable = !this.musicEnable;
	}

	public void toogleFx() {
		this.fxEnable = !this.fxEnable;
	}
	
}
